# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Renato Silva <br.renatosilva@gmail.com>

_realname=fontconfig
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.13.92
pkgrel=1
pkgdesc="A library for configuring and customizing font access (mingw-w64)"
arch=('any')
url="https://wiki.freedesktop.org/www/Software/fontconfig/"
license=("custom")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-python-lxml"
             "${MINGW_PACKAGE_PREFIX}-gperf")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-expat>=2.1.0"
         "${MINGW_PACKAGE_PREFIX}-freetype>=2.3.11"
         "${MINGW_PACKAGE_PREFIX}-bzip2>=1.0.6"
         "${MINGW_PACKAGE_PREFIX}-libiconv")
options=('staticlibs' 'strip')
install=${_realname}-${CARCH}.install
source=("https://www.freedesktop.org/software/fontconfig/release/fontconfig-${pkgver}.tar.xz"
        0001-fix-config-linking.all.patch
        0002-fix-mkdir.mingw.patch
        0004-fix-mkdtemp.mingw.patch
        0005-fix-setenv.mingw.patch
        0007-pkgconfig.mingw.patch)
sha256sums=('506e61283878c1726550bc94f2af26168f1e9f2106eac77eaaf0b2cdfad66e4e'
            '1266d4bbd8270f013fee2401c890f0251babf50a175a69d681d3a6af5003c899'
            '0d950eb8a19858bff1f0b26e4a560f589e79e7eb7f22f723267748dfe55e0b63'
            '57ff8420dbf62873b6fcb38b52fb7b37e0e278425a9125e15dccba54668c8ab9'
            '552b54010f9fe4097f332cf2397bbd3e78489542d3bbf07792ed1cfe9381796e'
            'af373531873da46d0356305da5444c1ec74f443cd2635ea2db6b7dadd1561f5b')

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}

  patch -p1 -i ${srcdir}/0001-fix-config-linking.all.patch
  patch -p1 -i ${srcdir}/0002-fix-mkdir.mingw.patch
  patch -p1 -i ${srcdir}/0004-fix-mkdtemp.mingw.patch
  patch -p1 -i ${srcdir}/0005-fix-setenv.mingw.patch
  patch -p1 -i ${srcdir}/0007-pkgconfig.mingw.patch

  autoreconf -fiv
}

build() {
  [[ -d "${srcdir}/build-${MINGW_CHOST}" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}"
  cd "${srcdir}/build-${MINGW_CHOST}"

  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --enable-shared \
    --enable-static \
    --enable-iconv \
    --with-libiconv=${MINGW_PREFIX} \
    --disable-docs \
    --with-arch=${CARCH} \
    --with-cache-dir=/var/cache/${_realname} \
    as_ln_s="cp -pR"

  sed -i 's,all-am: Makefile $(PROGRAMS),all-am:,' test/Makefile
  make
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make -j1 DESTDIR="${pkgdir}" install
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
  mv ${pkgdir}{,${MINGW_PREFIX}}/var

  find "${pkgdir}${MINGW_PREFIX}" -name '*.def' -o -name '*.exp' | xargs -rtl1 rm

  # Convert MSYS2-specific paths to pkg-config variables (see issue #872)
  sed -i "s#${MINGW_PREFIX}/lib#\${libdir}#g" "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig/fontconfig.pc
  sed -i "s#${MINGW_PREFIX}/include#\${includedir}#g" "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig/fontconfig.pc
}
